<?php

namespace console\controllers;

use common\helpers\HashidsHelper;
use common\models\bonus\Bonus;
use common\models\bonus\BonusDay;
use common\models\bonus\BonusDayMember;
use common\models\task\TaskList;
use Yii;
use yii\helpers\Console;
use yii\console\Controller;
use linslin\yii2\curl;
use yii\helpers\Json;

class TestController extends Controller
{
    /**
     * 初始化
     *
     * @throws \yii\base\Exception
     */
    public function actionOdds($odds1 , $odds2)
    {
        $money = 500;
        $all = ($money * $odds1 / $odds2) + $money; 
        if($all < $odds1 * $money){
            echo '满足条件,赚'. ($odds1 * $money - $all);
        }else{
            echo '不满足亏'. ($odds1 * $money - $all);
        }
    }

    public function actionLogin()
    {
        $curl = new curl\Curl();
        $response = $curl
        ->setHeaders([
            'User-Agent'=>'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
            'Origin'=>'https://raybet69.com',
            'Content-Type'=>'application/x-www-form-urlencoded'
        ])
        ->setOption(CURLOPT_SSL_VERIFYPEER, false)
        ->setPostParams([
            'account'=>'xp16889',
            'password'=>'aabb1122',
            'platform'=>1,
            'deviceId'=>'7cff04e43894cbc7b92e466ee390abf8'
        ])->post('https://api.365raylines.com/user/login');
        switch ($curl->responseCode) {

            case 'timeout':
                //timeout error logic here
                break;
                
            case 200:
                //success logic here
                break;
        
            case 404:
                //404 Error logic here
                break;
        }
        $data = Json::decode($response);
        $token = $data['data']['token'];
        echo $token;
        $this->webSocket($token);
        // var_dump($curl->responseCode);
        // var_dump($curl->errorCode);
        //list response headers
        // var_dump($curl->responseHeaders);

    }

    public function actionWebSocket($token = '52a734c9-7f03-4ff7-bb30-def954e1b9eb')
    {
        $client = new \Swoole\Client(SWOOLE_SOCK_TCP);
        if (!$client->connect('wss://socket.365raylines.com/socketcluster/')) {
            exit("connect failed. Error: {$client->errCode}\n");
        }
        $client->send('{"event":"#handshake","data":{"authToken":null},"cid":22}');
        $client->send('{"event":"#subscribe","data":{"channel":"match"},"cid":2}');
        $client->send('{"event":"user.login","data":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHBfa2V5IjoiY2QyNzZmZTQ5YmEyZTQ1YiIsImRhdGEiOnsidXNlcl9uYW1lIjoieHAxNjg4OSIsImxvYmJ5X3VybCI6Ii8jL2xvZ2luLyIsImlhdCI6IjYzNzYwMTc2NzExIn19.KgQ8GE99rooxH8lwHz-5rySXZC0VTk9TAub87vJP5gs","cid":3}');
        echo $client->recv();
        $client->close();
    }
    public function actionIndex()
    {
        echo HashidsHelper::encode(2);
        $x = '5n8yzD1Lkp';
        echo HashidsHelper::decode($x);
    }

    public function actionTask()
    {
        Yii::$app->queue->push(new \common\queues\BonusJob(['id' => 1]));

    }
}